Quick Start

Precondition: You have installed and created a project template

Check the configuration file

Templates default to PostgreSQL as a relational database and Redis as distributed cache support.

You need to configure the connection string in appsettings.jsonand replace the default database if you want.

Defining Solid Models

  • Defining a solid model under anEntityproject
  • Define database context andDbSetunderEntityFrameworkproject

Important

Please follow the official documentation ofEntity Framework Coreto define models and relationships.

Generate base code

Usedry studio to generatedto,Manager,Controller and other basic code.

Tip

For more information aboutdry cli, seethe dry cli documentation.

Implement custom business logic

Implement business logic in theApplication``Managerdirectory, usually including Filter Query, Add Entity, and Update Entity.

Filter query

Steps to build custom query criteria:

  1. Construct your own query condition Queryable
  2. Call FilterAsync<T> method to get results

Code example:

public override async Task<PageList<DiscussItemDto>> FilterAsync(DiscussFilterDto filter)
{
    // 根据实际业务构建筛选条件
    if (filter.IsTop != null)
    {
        Queryable = Queryable.Where(q => q.IsTop == filter.IsTop);
    }
    if (filter.MemberId != null)
    {
        Queryable = Queryable.Where(q => q.Member.Id == filter.MemberId);
    }
    return await Query.FilterAsync<DiscussItemDto>(Queryable, filter.PageIndex, filter.PageSize);
}

Additional entity

Code example:

public override async Task<Discuss> AddAsync(Discuss entity)
{
    var res = await Command.CreateAsync(entity);
    // TODO: do something
    // 提交更新
    await Command.SaveChangeAsync();
    return res;
}

Updating the entity

Manager provides a GetCurrentAsync method to get the current (writable dbcontext) entity.

In the controller, the entity will be obtained first. If it does not exist, it will directly return to404.

The entity update method passes two parameters, one for the entity itself and one for the submittedDTO object. The entity itself is obtained by usingGetCurrentAsync method in the controller, and can be passed directly as a parameter.

Code example:

The following aspects demonstrate how to update associated content.

public override async Task<ResourceTypeDefinition> UpdateAsync(ResourceTypeDefinition entity, ResourceTypeDefinitionUpdateDto dto)
    {
        // 更新关联的内容
        entity!.AttributeDefines = null;
        if (dto.AttributeDefineIds != null)
        {
            // 通过父类的 Stores 查询其他实体的内容
            var attributeDefines = await Stores.ResourceAttributeDefineCommand.Db.Where(a => dto.AttributeDefineIds.Contains(a.Id)).ToListAsync();
            entity.AttributeDefines = attributeDefines;
        }
        return await base.UpdateAsync(entity, dto);
    }

For more information

Managerprovides default query method for details, which can directly pass query conditions.

If you customize queries, such as querying associated content, you need to add new methods to implement them.

Managerprovides Query.Db members to query directly against the current model.

Code example:

public async Task<ResourceGroup?> FindAsync(Guid id)
{
    return await Query.Db
        .Include(g => g.Environment)
        .FirstOrDefaultAsync(g => g.Id == id);
}

Deletion processing

Delete defaults to soft delete, and if you want to modify this behavior, setEnableSoftDelete to false in your Manager class. like:

Command.EnableSoftDelete = false;

Deletion sometimes involvesassociation deletion, example code:

public override async Task<Resource?> DeleteAsync(Guid id)
{
    var resource = entity;
    if (resource!.Attributes != null)
    {
        Stores.CommandContext.RemoveRange(resource.Attributes);
    }
    return await DeleteAsync(resource);
}

Call business logic in controller

After you implement the business interface and implementation inApplication, you can call the related business methods in the interface class inHttp.APIthrough dependency injection.

Note

Please keep the code of the controller clean and avoid查询数据库、实现业务逻辑directly in the controller.

Perform the migration

After you add or modify entities, you need to perform a database migration for the database structure changes to take effect.

You can usedotnet ef command to generate migration scripts.

Important

Database migration is required underHttp.APIproject.

Or you can simply execute the migration generationscript/EFMigrations.ps1we provide.

Tip

You don't need to executedotnet ef update because the program commits changes automatically when it starts.